{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LlamaParse - Fast checking Insurance Contract for Coverage\n",
    "\n",
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_cloud_services/blob/main/examples/parse/demo_insurance.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
    "\n",
    "In this notebook we will look at how LlamaParse can be used to extract structured coverage information from an insurance policy.\n",
    "\n",
    "Status:\n",
    "| Last Executed | Version | State      |\n",
    "|---------------|---------|------------|\n",
    "| Aug-19-2025   | 0.6.61  | Deprecated |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Installation of required packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install \"llama-index>=0.13.0<0.14.0\" llama-parse"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Download an insurance policy from IRDAI\n",
    "\n",
    "The Insurance Regulatory and Development Authority of India (IRDAI) maintains a great resource: https://policyholder.gov.in/web/guest/non-life-insurance-products where all insurance policies available in India are publicly available for download! Let's download a complex health insurance policy as an example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!wget \"https://policyholder.gov.in/documents/37343/931203/NBHTGBP22011V012223.pdf/c392bcc1-f6a8-cadd-ab84-495b3273d2c3?version=1.0&t=1669350459879&download=true\" -O \"./policy.pdf\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Initializing LlamaIndex and LlamaParse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"LLAMA_CLOUD_API_KEY\"] = \"llx-...\"\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-...\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms.openai import OpenAI\n",
    "from llama_index.embeddings.openai import OpenAIEmbedding\n",
    "from llama_index.core import VectorStoreIndex\n",
    "from llama_index.core import Settings\n",
    "\n",
    "# for the purpose of this example, we will use the small model embedding and gpt3.5\n",
    "embed_model = OpenAIEmbedding(model=\"text-embedding-3-small\")\n",
    "llm = OpenAI(model=\"gpt-5-mini\")\n",
    "\n",
    "Settings.llm = llm\n",
    "Settings.embed_model = embed_model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Vanilla Approach - Parse the Policy with LlamaParse into Markdown"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Started parsing the file under job_id 35052045-ce36-4343-9e7c-11e059a59cc2\n"
     ]
    }
   ],
   "source": [
    "from llama_cloud_services import LlamaParse\n",
    "\n",
    "result = await LlamaParse().aparse(\"./policy.pdf\")\n",
    "documents = result.get_markdown_documents(split_by_page=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bupa niva Health Insurance\n",
      "\n",
      "# 1. Preamble\n",
      "\n",
      "This ‘Travel Infinity’ Policy is a contract of insurance between You and Us which is subject to payment of full premium in advance and the terms, conditions and exclusions of this Policy. Expense incurred outside the policy period will NOT be covered. Unutilized Sum Insured will expire at the end of policy year. All applicable benefits, details and limits are mentioned in your Certificate of insurance. We will cover only allopathic treatments in this policy.\n",
      "\n",
      "# 2. Defined Terms\n",
      "\n",
      "The terms listed below in this Section and used elsewhere in the Policy in Initial Capitals shall have the meaning set out against them in this Section.\n",
      "\n",
      "# Standard Definitions\n",
      "\n",
      "# 2.1\n",
      "\n",
      "Accident or Accidental means sudden, unforeseen and involuntary event caused by external, visible and violent means.\n",
      "\n",
      "# 2.2\n",
      "\n",
      "Co-payment means a cost sharing requirement under a health insurance policy that provides that the policyholder/insured will bear a specified percentage of the adm\n"
     ]
    }
   ],
   "source": [
    "print(documents[0].text[0:1000])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "index = VectorStoreIndex.from_documents(documents)\n",
    "query_engine = index.as_query_engine()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Querying the model for coverage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I can’t give an exact dollar amount without the values shown on your Certificate of Insurance. How the claim would be settled:\n",
      "\n",
      "1. First check that your policy’s required delay threshold is met (the policy only pays if the delay exceeds the number of hours shown on your Certificate). Also the insurer won’t pay if the delay was publicly known at least 6 hours before departure.\n",
      "\n",
      "2. Find which benefit option applies on your Certificate: a fixed payment or reimbursement of actual alternate-travel cost.\n",
      "   - If a fixed payment applies, you will receive the fixed sum listed on the Certificate (regardless of the $450 you paid), subject to the other conditions and any deductible shown.\n",
      "   - If reimbursement applies, the insurer will reimburse up to the Sum Insured shown on the Certificate, but will first deduct any compensation paid by the airline or other sources and then apply the deductible.\n",
      "\n",
      "3. Reimbursement formula (if reimbursement option applies):\n",
      "   Payable = max(0, min(Sum Insured, Amount you paid ($450) − airline/other compensation) − Deductible)\n",
      "\n",
      "4. Other limits: only one flight-delay claim is payable in the policy period as shown on the Certificate.\n",
      "\n",
      "Example: if your Certificate shows Sum Insured $1,000, Deductible $50, and the airline paid no compensation, payable = min(1000,450) − 50 = $400.\n",
      "\n",
      "Check your Certificate of Insurance for the delay threshold, whether fixed or reimbursement applies, the Sum Insured and the Deductible, and any airline compensation already received to calculate the exact amount.\n"
     ]
    }
   ],
   "source": [
    "query_1 = \"My flight was delayed 8 hours and I paid $450, how much am I covered for?\"\n",
    "\n",
    "response_1 = await query_engine.aquery(query_1)\n",
    "print(str(response_1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The information is split across the document which leads to retrieval issues. Let's try some parsing instructions to improve our result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Started parsing the file under job_id c89abe4b-0bb3-4e04-a37f-1da880392346\n",
      "."
     ]
    }
   ],
   "source": [
    "result = await LlamaParse(\n",
    "    result_type=\"markdown\",\n",
    "    system_prompt_append=\"\"\"\n",
    "This document is an insurance policy.\n",
    "When a benefits/coverage/exlusion is describe in the document amend to it add a text in the following benefits string format (where coverage could be an exclusion).\n",
    "\n",
    "For {nameofrisk} and in this condition {whenDoesThecoverageApply} the coverage is {coverageDescription}. \n",
    "                                        \n",
    "If the document contain a benefits TABLE that describe coverage amounts, do not output it as a table, but instead as a list of benefits string.\n",
    "                                       \n",
    "\"\"\",\n",
    ").aparse(\"./policy.pdf\")\n",
    "\n",
    "documents_with_instruction = result.get_markdown_documents(split_by_page=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let see how the 2 parsing compare (change target page to explore)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Inpatient treatment\n",
      "\n",
      "# Claim Form (filled and signed by the Insured)\n",
      "\n",
      "# Hospital Daily Cash\n",
      "\n",
      "# Release of Medical information Form (filled and signed by the Insured)\n",
      "\n",
      "# Waiver of Deductible\n",
      "\n",
      "# Original pathological and diagnostic reports, discharge summary indoor case papers (if any) and prescriptions issued by the treating Medical practitioner or Network Provider\n",
      "\n",
      "# Adventure Sports Cover\n",
      "\n",
      "# Home to Home Cover\n",
      "\n",
      "# Extension to in-patient care\n",
      "\n",
      "# Ambulance Charge\n",
      "\n",
      "# Out-patient treatment\n",
      "\n",
      "# Cancer Screening &#x26; Mammographic Examination\n",
      "\n",
      "# New Born baby Cover\n",
      "\n",
      "# Maternity\n",
      "\n",
      "# Complete pre-existing disease cover\n",
      "\n",
      "# Medical sum insured replenishment in case of hospitalization due to accident\n",
      "\n",
      "# Waiver of sublimit for insured above 60 years of age\n",
      "\n",
      "# Psychiatric Counseling\n",
      "\n",
      "# Physiotherapy\n",
      "\n",
      "# Terrorism cover\n",
      "\n",
      "# Medical tele-consultation\n",
      "\n",
      "# Medical evacuation\n",
      "\n",
      "Medical reports and transportation details issued by the evacuation agency, prescriptions and medical report by the attending Medical Practitioner furnishing the name of the Insured Person and details of treatment rendered along with the statement confirm the necessity of evacuation. Documentary proof for expenses incurred towards the Medical Evacuation.\n",
      "\n",
      "# Compassionate visit\n",
      "\n",
      "A certificate from the Medical Practitioner recommending the presence in the form of special assistance to be rendered by an additional member during the entire period of hospitalization. The certificate shall also specify the minimum period in which person is admitted in the hospital. Discharge summary of the Hospital furnishing details including the date of admission and date of discharge. Stamped boarding pass with invoice used for the travel by the Immediate Family Member. Copy passport of Immediate Family Member with entry and exit stamp.\n",
      "\n",
      "# Escort of Minor Child\n",
      "\n",
      "A certificate from the Medical Practitioner specifying the minimum period of Hospitalization. Discharge summary of the Hospital furnishing details including the date of admission and date of discharge, Stamped Boarding pass used for the return travel of the child to the Country of Residence. Stamped Boarding pass of the attendant from the Country of Residence to the place of hospitalization (if attendant is necessary). Copy of passport of the child with entry and exit stamp.\n",
      "\n",
      "# Upgradation to Business Class\n",
      "\n",
      "A certificate from the Medical Practitioner specifying the minimum period of Hospitalization. Discharge summary of the Hospital furnishing the details including the date of admission and date of discharge.\n",
      "\n",
      "Product Name: Travel infinity\n",
      "\n",
      "Product UIN: NBHTGBP22011V012223\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "=========================================================\n",
      "\n",
      "\n",
      "\n",
      "# Claim Form\n",
      "\n",
      "Inpatient treatment\n",
      "\n",
      "- Claim Form (filled and signed by the Insured)\n",
      "- Release of Medical information Form (filled and signed by the Insured)\n",
      "- Original pathological and diagnostic reports, discharge summary indoor case papers (if any) and prescriptions issued by the treating Medical practitioner or Network Provider\n",
      "- Passport and Visa copy with Entry Stamp of Country of Visit and exit Stamp from India\n",
      "- FIR report of police (if applicable)\n",
      "\n",
      "Hospital Daily Cash\n",
      "\n",
      "Waiver of Deductible\n",
      "\n",
      "Optional Co-payment\n",
      "\n",
      "Adventure Sports Cover\n",
      "\n",
      "Home to Home Cover\n",
      "\n",
      "Extension to in-patient care\n",
      "\n",
      "Ambulance Charge\n",
      "\n",
      "Out-patient treatment\n",
      "\n",
      "Cancer Screening &#x26; Mammographic Examination\n",
      "\n",
      "New Born baby Cover\n",
      "\n",
      "Maternity\n",
      "\n",
      "Complete pre-existing disease cover\n",
      "\n",
      "Medical sum insured replenishment in case of hospitalization due to accident\n",
      "\n",
      "Waiver of sublimit for insured above 60 years of age\n",
      "\n",
      "Psychiatric Counseling\n",
      "\n",
      "Physiotherapy\n",
      "\n",
      "Terrorism cover\n",
      "\n",
      "Medical tele-consultation\n",
      "\n",
      "Medical evacuation\n",
      "\n",
      "Medical reports and transportation details issued by the evacuation agency, prescriptions and medical report by the attending Medical Practitioner furnishing the name of the Insured Person and details of treatment rendered along with the statement confirming the necessity of evacuation. Documentary proof for expenses incurred towards the Medical Evacuation.\n",
      "\n",
      "Compassionate visit\n",
      "\n",
      "A certificate from the Medical Practitioner recommending the presence in the form of special assistance to be rendered by an additional member during the entire period of hospitalization. The certificate shall also specify the minimum period in which the person is admitted in the hospital. Discharge summary of the Hospital furnishing details including the date of admission and date of discharge. Stamped boarding pass with invoice used for the travel by the Immediate Family Member. Copy passport of Immediate Family Member with entry and exit stamp.\n",
      "\n",
      "Escort of Minor Child\n",
      "\n",
      "A certificate from the Medical Practitioner specifying the minimum period of Hospitalization. Discharge summary of the Hospital furnishing details including the date of admission and date of discharge, Stamped Boarding pass used for the return travel of the child to the Country of Residence. Stamped Boarding pass of the attendant from the Country of Residence to the place of hospitalization (if attendant is necessary). Copy of passport of the child with entry and exit stamp.\n",
      "\n",
      "Upgradation to Business Class\n",
      "\n",
      "A certificate from the Medical Practitioner specifying the minimum period of Hospitalization. Discharge summary of the Hospital furnishing the details including the date of admission and date of discharge.\n",
      "\n",
      "Product Name: Travel infinity\n",
      "\n",
      "Product UIN: NBHTGBP22011V012223\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "target_page = 45\n",
    "\n",
    "print(documents[target_page].text)\n",
    "print(\"\\n\\n=========================================================\\n\\n\")\n",
    "print(documents_with_instruction[target_page].text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "instruction_index = VectorStoreIndex.from_documents(documents_with_instruction)\n",
    "query_engine_instruction = instruction_index.as_query_engine()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparing Instruction-Augmented Parsing vs. Vanilla Parsing\n",
    "\n",
    "When we parse the document with natural language instructions to add context on insurance coverage, we are able to correctly answer a wide range of queries in our RAG pipeline. In contrast, a RAG pipeline built with the vanilla method is not able to answer these queries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vanilla:\n",
      "I can’t give an exact payout without details from your Certificate of Insurance. What matters is which benefit applies and the certificate values. Here’s how to determine the amount and some examples:\n",
      "\n",
      "What to check on your certificate (send these if you want a precise calculation)\n",
      "- Which benefit is being used: Flight Delay (alternate travel booking reimbursement or fixed amount) or Trip Delay (fixed amount per block of hours).  \n",
      "- The minimum delay threshold (the number of hours the delay must exceed).  \n",
      "- Whether the policy pays reimbursement or a fixed amount (and, if fixed, the amount per block and length of each block).  \n",
      "- Sum Insured / maximum limit for that benefit.  \n",
      "- Deductible (amount you must absorb per claim).  \n",
      "- Any compensation already paid by the airline or other source (this is deducted from the insurer’s payment).  \n",
      "- Reason for the delay and whether it’s an excluded reason (e.g., delay was publicly known 6+ hours before departure).\n",
      "\n",
      "How to calculate (general rules)\n",
      "- If the policy reimburses actual alternate travel costs: insurer pays up to the Sum Insured, but subtract any compensation from the carrier and subtract the deductible. Payment = min(Sum Insured, your expense) − carrier compensation − deductible.\n",
      "- If the policy pays a fixed amount per block of hours: determine how many blocks your 8-hour delay covers (e.g., if a block is 4 hours, 8 hours = 2 blocks). Payment = blocks × fixed amount (subject to any stated maximum and any applicable deductible/offsets).\n",
      "\n",
      "Two simple examples\n",
      "- Reimbursement example: Sum Insured ≥ $450, deductible $50, airline paid $0 → insurer would pay $450 − $50 = $400.  \n",
      "- Fixed-per-block example: certificate pays $100 per 4-hour block. 8 hours = 2 blocks → insurer would pay 2 × $100 = $200 (subject to any max limit or deductible if applicable).\n",
      "\n",
      "If you share the certificate values (which benefit, sum insured, deductible, fixed-per-block amount if any, and any airline compensation), I’ll compute the exact amount.\n",
      "With instructions:\n",
      "The amount payable depends on the Trip Delay benefit sum insured you chose in your policy certificate. Available Trip Delay benefit options are: 1K, 2K, 3K, 4K, 5K, 7.5K, 10K, 15K and 20K. The insurer pays the selected benefit amount for each block of delay hours as defined in your certificate (maximum up to 24 hours).\n",
      "\n",
      "So:\n",
      "- If your chosen Trip Delay benefit is at least equal to $450, the policy can cover your $450 expense (subject to the policy terms and exclusions).\n",
      "- If your chosen benefit is less than $450, the insurer will pay only up to the chosen benefit amount.\n",
      "\n",
      "Check your certificate to confirm which Trip Delay sum insured you purchased and whether any exclusions (for example, delays announced ≥6 hours before departure) apply.\n"
     ]
    }
   ],
   "source": [
    "query_1 = \"My flight was delayed 8 hours and I paid $450, how much am I covered for?\"\n",
    "\n",
    "response_1 = await query_engine.aquery(query_1)\n",
    "print(\"Vanilla:\")\n",
    "print(response_1)\n",
    "\n",
    "print(\"With instructions:\")\n",
    "response_1_i = await query_engine_instruction.aquery(query_1)\n",
    "print(response_1_i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Looking at the policy it says in list I that one expense not covered is Baby food"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vanilla:\n",
      "No. Food and beverages (including baby food) are excluded as expenses not linked to treatment. The policy only covers medical treatment and specified newborn items (e.g., emergency inpatient care and vaccinations — vaccinations limited to USD 500) and explicitly excludes \"baby charges\" unless specifically indicated.\n",
      "With instructions:\n",
      "No. Baby food is not covered. The policy pays medical treatment expenses and expressly excludes items not linked to treatment (for example food and beverages), and it also lists \"baby charges\" as not payable unless specifically indicated.  \n",
      "\n",
      "Newborn medical treatment and vaccinations can be covered under the newborn/maternity benefits (vaccination cover is limited and subject to the policy's special conditions, waiting periods and deductibles), so check your certificate of insurance for any specific limits or endorsements.\n"
     ]
    }
   ],
   "source": [
    "query_2 = \"I just had a baby, is baby food covered?\"\n",
    "\n",
    "response_2 = await query_engine.aquery(query_2)\n",
    "print(\"Vanilla:\")\n",
    "print(response_2)\n",
    "\n",
    "print(\"With instructions:\")\n",
    "response_2_i = await query_engine_instruction.aquery(query_2)\n",
    "print(response_2_i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vanilla:\n",
      "Gauze (including gauze soft) used in your operation is included within the procedure charges. It is subsumed into the surgical/procedure fee and is not payable as a separate item.\n",
      "With instructions:\n",
      "Gauze used during your operation is included in the procedure charges. Its cost is subsumed into the procedure/surgical fee and will not be reimbursed as a separate line item.\n"
     ]
    }
   ],
   "source": [
    "query_3 = \"How is gauze used in my operation covered?\"\n",
    "\n",
    "response_3 = await query_engine.aquery(query_3)\n",
    "print(\"Vanilla:\")\n",
    "print(response_3)\n",
    "\n",
    "print(\"With instructions:\")\n",
    "response_3_i = await query_engine_instruction.aquery(query_3)\n",
    "print(response_3_i)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
